﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MediaRecommender.Model;
using MediaRecommender.Controller;

namespace MediaRecommender
{
    public partial class Books : System.Web.UI.Page
    {
        private string emptyCover = "http://thumbs.dreamstime.com/m/book-empty-cover-white-28912004.jpg";
        protected string tryOut = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateRecommendationsController grc = new GenerateRecommendationsController();
                string currUsername = (string)Session["Username"];
                User u = UserCollection.Instance.GetUser(currUsername);
                foreach (Media m in grc.CreateRecommendations(u))
                {
                    //construct table cell
                    ImageButton imgButton = new ImageButton();
                    TextBox textbox = new TextBox();
                    TableCell td = new TableCell();
                    imgButton.Attributes["style"] = "float:buttom";
                    textbox.Attributes["style"] = "position:relative";
                    textbox.Attributes["style"] = "float:top";
                    textbox.Width = 214;
                    textbox.Attributes["style"] = "background-color:#e0ffff";
                    textbox.ReadOnly = true;


                    textbox.Text = m.Title; td.Text = m.Title;
                    if (m.CoverUrl.Length != 0 && m.CoverUrl != null)
                    {
                        imgButton.ImageUrl = m.CoverUrl;
                    }
                    else imgButton.ImageUrl = emptyCover;

                    imgButton.Width = 214;
                    imgButton.Height = 350;
                    imgButton.Enabled = false;
                    imgButton.ToolTip = showInfo(m);
                    td.Controls.Add(textbox);
                    td.Controls.Add(imgButton);
                    //

                    //determain wether or not a new line is needed and then adds it and the cell or just the cell
                    TableRowCollection trList = TableHeader.Rows;
                    int count = trList.Count; //needed to acces the last row

                    if (trList[count - 1].Cells.Count > 3)
                    {
                        TableRow tr = new TableRow();
                        tr.Cells.Add(td);
                        TableHeader.Rows.Add(tr);
                    }
                    else
                    {
                        trList[count - 1].Cells.Add(td);
                    }
                }
            }
        }

        private string showInfo(Media m)
        {
            string infoToReturn = "";

            infoToReturn += "Genre: ";
            foreach (Genre g in m.Genres)
            {
                infoToReturn += Enum.GetName(typeof(Genre), g) + ", ";
            }

            if (m is VideoGame)
            {
                VideoGame VG = (VideoGame)m;
                infoToReturn += "\nPlatforms: ";
                foreach (Platform p in VG.Platforms)
                    infoToReturn += Enum.GetName(typeof(Platform), p) + ", ";
                infoToReturn += "\nPublisher: " + VG.Publisher.Name;
                infoToReturn += "\nStudio: " + VG.Studio.Name;
            }
            else if (m is Movie)
            {
                Movie MO = (Movie)m;
                int maxActors = 5;
                int count = 0;
                infoToReturn += "\nActors: ";
                foreach (Actor a in MO.Actors)
                {
                    infoToReturn += a.Name + ", ";
                    count++;
                    if (maxActors < count) break;
                }
                infoToReturn += "\nDirector: " + MO.Director.Name;
            }
            else
            {
                Book B = (Book)m;
            }
            return infoToReturn;
        }

    }
}